//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner.ActivityImportWizards
{
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Windows;
    using Microsoft.Research.ScientificWorkflow.TridentModel;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using System;

    /// <summary>
    /// 
    /// </summary>
    public partial class WSActivityCustomization : IActivityWizardScreens
    {
        private WSWizardPresenter WSPresenter = null;
        private string validationError = string.Empty;
        private bool isValid = true;

        /// <summary>
        /// Initializes a new instance of the <see cref="WSActivityCustomization"/> class.
        /// </summary>
        /// <param name="presenter">The presenter.</param>
        public WSActivityCustomization(WSWizardPresenter presenter)
        {
            this.InitializeComponent();

            this.DataContext = presenter.WSModel;
            WSPresenter = presenter;
        }

        /// <summary>
        /// Validates activity parameter name.
        /// </summary>
        /// <returns></returns>
        private bool ValidateInputs()
        {
            isValid = true;
            Regex checkSpecialChar = new Regex(@"[^\w]");
            const int parameterMaximuLength = 75;

            foreach (WebMethodParameterInfo parameter in WSPresenter.WSModel.SelectedWebMethodParamList)
            {
                if (string.IsNullOrEmpty(parameter.ActivityParamName))
                    parameter.ActivityParamName = parameter.WebMethodParamName;



                // Check for special character.
                if (checkSpecialChar.IsMatch(parameter.ActivityParamName))
                {
                    isValid = false;
                    validationError = TridentResourceManager.GetString("ParameterNameSpclCharError");
                    break;
                }

                // Check for unique name.
                var matchQuery = from parameterInList in WSPresenter.WSModel.SelectedWebMethodParamList
                                 where parameterInList.ActivityParamValidName == parameter.ActivityParamValidName
                                 select parameterInList;

                if (matchQuery.Count() > 1)
                {
                    isValid = false;
                    validationError = TridentResourceManager.GetString("ParameterNameSameError");
                    break;
                }

                // Check for maximum lenght
                if (parameter.ActivityParamName.Length > parameterMaximuLength)
                {
                    isValid = false;
                    validationError = TridentResourceManager.GetString("ParameterLengthError", parameterMaximuLength);
                    break;
                }
            }

            // Check for actvity Name
            if (checkSpecialChar.IsMatch(WSPresenter.WSModel.ActivityName))
            {
                isValid = false;
                validationError = TridentResourceManager.GetString("ActivityNameSpclCharError");
            }

            // Show MessageBox with OK.
            if (!isValid)
                TridentMessageBox.ShowTridentErrorMessageBox(validationError);

            return isValid;
        }

        /// <summary>
        /// Called when [click next].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OnClickNext(object sender, RoutedEventArgs e)
        {
            if (ValidateInputs())
            {
                if (null != NextButtonEventHandler)
                {
                    NextButtonEventHandler(sender, new ActivityImportEventArgs((new WSActivityPreview(WSPresenter))));
                }
            }
        }

        /// <summary>
        /// Called when [click cancel].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OnClickCancel(object sender, RoutedEventArgs e)
        {
            if (null != CancelButtonEventHandler)
            {
                if (TridentMessageBox.ShowTridentConfirmationYesNoMessageBox(
                    TridentResourceManager.GetString("WizardCloseMessage")) == System.Windows.MessageBoxResult.Yes)
                    CancelButtonEventHandler(sender, e);
            }
        }

        /// <summary>
        /// Called when [click back].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OnClickBack(object sender, RoutedEventArgs e)
        {
            if (null != BackButtonEventHandler)
            {
                BackButtonEventHandler(sender, e);
            }
        }

        /// <summary>
        /// Called when [click browse].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OnClickBrowse(object sender, RoutedEventArgs e)
        {
            WSPresenter.BrowseIconFile();
        }

        /// <summary>
        /// Handles the SelectedItemChanged event of the viewsTreeView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedPropertyChangedEventArgs&lt;System.Object&gt;"/> instance containing the event data.</param>
        void viewsTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            WSPresenter.WSModel.SelectedCategory = (sender as System.Windows.Controls.TreeView).SelectedItem as Category;
            WSPresenter.WSModel.ActivityCategory = string.Empty;
            WSPresenter.GetActivityCategory(WSPresenter.WSModel.SelectedCategory);
        }

        void ItemSelected(object sender, RoutedEventArgs e)
        {
            (sender as System.Windows.Controls.TreeViewItem).BringIntoView();
        }

        #region IActivityWizardScreens Members

        /// <summary>
        /// Occurs when [Next button handler].
        /// </summary>
        public event EventHandler<ActivityImportEventArgs> NextButtonEventHandler;

        /// <summary>
        /// Occurs when [Back button handler].
        /// </summary>
        public event EventHandler BackButtonEventHandler;

        /// <summary>
        /// Occurs when [Cancel button handler].
        /// </summary>
        public event EventHandler CancelButtonEventHandler;

        /// <summary>
        /// Handles move request.
        /// </summary>
        public event EventHandler OnWindowMoveEventHandler;

        #endregion

        /// <summary>
        /// Called when mouse left button down.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void OnMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (this.OnWindowMoveEventHandler != null)
            {
                this.OnWindowMoveEventHandler(sender, e);
            }
        }
    }
}